import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
    // State提供了唯一的公共数据源，所有共享的数据都要统一放在Store的State中进行存储
    state: {
        profile: {},
        imageUrl: "",
        enterpriseProfile: {},
        webSocketList: []
    },
    // Mutation用于变更Store中的数据
    mutations: {
        setProfile(state, profile) {
            state.profile = profile
        },
        setImageUrl(state, url) {
            state.imageUrl = url
        },
        setProfileState: (state, profileState) => {
            state.profile.state = profileState
        },
        setEnterpriseProfile(state, profile) {
            state.enterpriseProfile = profile
        },
        setWebSocket(state, webSocket) {
            state.webSocketList.push(webSocket)
            // {
            //     sessionId: sessionId,
            //     webSocket : webSocket
            // }
        }
    },
    // Action用于处理异步任务
    actions: {

    },
    // Getter用于对Store中的数据进行加工处理形成新的数据，类似Vue中的计算属性
    getters: {
        getNickName: state => {
            return state.profile.nickName
        },
        getProfile: state => {
            return state.profile
        },
        getWebSocket: state => {
            return function (sessionId) {
                return state.webSocketList.find(item => {
                    return item.sessionId == sessionId
                }).webSocket
            }
        },
        isWebSocketExist: state => {
            return function (sessionId) {
                for (let ws of state.webSocketList) {
                    if (ws.sessionId == sessionId) {
                        return true
                    }
                }
                return false
            }
        }
    }
})
